<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!showAddress">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="showAddress">
        <li><span>收<i/>货<i/>人：</span>{{showAddress.receiver}}</li>
        <li><span>联系方式：</span>{{showAddress.contact.replace(/^(\d{3})\d{4}(\d{4})$/,'$1****$2')}}</li>
        <li><span>收货地址：</span>{{showAddress.fullLocation.replace(/ /g,'')}}{{showAddress.address}}</li>
      </ul>
      <a v-if="showAddress" href="javascript:;" @click="openAddressEdit(showAddress)">修改地址</a>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="openDialog">切换地址</XtxButton>
      <XtxButton class="btn" @click="openAddressEdit({})">添加地址</XtxButton>
    </div>
  </div>

  <!-- 切换地址提示框 -->
  <XtxDialog title="切换收货地址" v-model:visible="visibleDialog">
    <!-- 主体-->
    <div  class="text item" :class="{active:selectedAddress&&selectedAddress.id===item.id}" v-for="item in list" :key="item.id" @click="selectedAddress=item">
      <ul>
        <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
        <li><span>联系方式：</span>{{item.contact.replace(/^(\d{3})\d{4}(\d{4})$/,'$1****$2')}}</li>
        <li><span>收货地址：</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
      </ul>
    </div>
    <template #footer>
      <XtxButton type="gray" style="margin-right:20px" @click="visibleDialog=false">取消</XtxButton>
      <XtxButton type="primary" @click="confirmAddressFn">确认</XtxButton>
    </template>
  </XtxDialog>

  <!-- 添加、编辑地址提示框 -->
  <AddressEdit ref="addressEditCom" @on-success="successHandler"></AddressEdit>
</template>
<script>
import { ref } from 'vue'
import AddressEdit from './address-edit'
export default {
  name: 'CheckoutAddress',
  components: { AddressEdit },
  props: {
    // 收货地址列表
    list: {
      type: Array,
      default: () => []
    }
  },
  // 1. 在拥有根元素的组件中，触发自定义事件，有没有emits选项无所谓
  // 2. 如果你的组件渲染的代码片段，vue3.0规范，需要在emits中申明你所触发的自定义事件
  // 3. 提倡：你发了自定义事件，需要在emits选项申明下，代码可读性很高
  emits: ['change'],
  setup (props, { emit }) {
    // 定义默认收货地址初始数据
    const showAddress = ref(null)
    const defaultAddress = props.list.find(item => item.isDefault === 0)
    if (defaultAddress) {
      showAddress.value = defaultAddress
    } else {
      if (props.list.length) {
        // eslint-disable-next-line vue/no-setup-props-destructure
        showAddress.value = props.list[0]
      }
    }

    // 切换地址对话框显示、隐藏的数据
    const visibleDialog = ref(null)

    // 组件初始化时传收货地址ID给父组件，如果没有则传空
    emit('change', showAddress.value && showAddress.value.id)

    // 选中的地址
    const selectedAddress = ref(null)
    // 切换地址的事件
    const confirmAddressFn = () => {
      showAddress.value = selectedAddress.value
      // 传选中的地址ID给父组件
      // 问号是es6语法，当selectedAddress.value有值时再寻找ID
      emit('change', selectedAddress.value?.id)
      visibleDialog.value = false
    }
    // 显示切换地址对话框的事件
    const openDialog = () => {
      // 先重置再显示对话框
      selectedAddress.value = null
      visibleDialog.value = true
    }

    // 显示添加、编辑地址对话框的事件
    const addressEditCom = ref(null)
    const openAddressEdit = (address) => {
      // 通过ref调用子组件方法，显示对话框
      // 如果address有值是显示编辑地址对话框，反之是添加地址对话框
      addressEditCom.value.open(address)
    }

    // 接收子组件传过来的地址数据
    const successHandler = (formData) => {
      const address = props.list.find(item => item.id === formData.id)
      if (address) {
        // 编辑收货地址逻辑
        for (const key in formData) {
          address[key] = formData[key]
        }
      } else {
        // 添加地址的操作
        // 如果是添加：往list中追加一条
      // 当你在修改formData的时候，数组中的数据也会更新，因为是同一引用地址。
      // 啥时候修改formData，当你打开对话框需要清空之前的输入信息
      // 克隆formData数据
        const jsonStr = JSON.stringify(formData)
        // eslint-disable-next-line vue/no-mutating-props
        props.list.unshift(JSON.parse(jsonStr))
      }
    }

    return {
      showAddress,
      visibleDialog,
      selectedAddress,
      confirmAddressFn,
      openDialog,
      openAddressEdit,
      addressEditCom,
      successHandler
    }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
